<template>
  <el-dialog
    :title="title"
    :visible.sync="isMapList.isMap"
    :before-close="resetFun"
    width="806px"
  >
    <div id="map" class="map-div">
      <AmapSearchBox
        class="search-box"
        v-if="map && showSearch"
        :on-search-result="onMapSearchResult"
        maxHeight="320px"
        searchSize="newNormal"
        :placeTitle="placeTitle"
      ></AmapSearchBox>
      <el-tooltip content="测距" placement="top" v-if="ifMapRuler">
        <div
          class="map-button-icon"
          :class="mapRulerFlag ? 'checked' : ''"
          @click="turnMapRuler"
        >
          <i class="icon iconfont icon-juliceliang"></i>
        </div>
      </el-tooltip>
      <div class="lon-lat-input" v-if="lonlatContent">
        <span class="lon-lat-text"
          >{{ addressData.longitude }},{{ addressData.latitude }}</span
        >
      </div>
      <div v-if="isMap">
        <map-type :mapMap="mapMap" :location="'auto auto 45px 16px'"></map-type>
        <map-zoom :map="map" :location="'auto auto 45px 110px'"></map-zoom>
      </div>
    </div>
    <div v-if="showBtn" slot="footer" class="dialog-footer">
      <el-button @click="submitFun" class="submitClass">确定</el-button>
      <el-button @click="resetFun" class="restClass">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import positionIcon from "@/assets/image/position-new@2x.png";
import { $v } from "@/utils/public";
import AmapSearchBox from "@/components/vueAmap/amap-search-box.vue";
import {
  addMarker,
  addScaleLine,
  initCTMap,
  markerPicking,
  measureClass,
  poiContraryGeocodingQuery,
  setZoomAndCenter,
} from "@/assets/common/openLayerCommon";
import MapType from "@/components/openlayer/mapType.vue";
import MapZoom from "@/components/openlayer/mapZoom.vue";
import "@ct/ct_map_ol/index.css";
let measure = null;

export default {
  components: { MapZoom, MapType, AmapSearchBox },
  name: "eventAddMap",
  data() {
    return {
      map: null, //地图
      mapMap: null, //地图
      isMap: false, //地图
      mapRulerFlag: false,
      centers: this.centerDefault, //中心点 当前北京 偏移量以及缩放调整
      mapSearchOption: {
        city: "全国",
        citylimit: true,
      },
      placeSearch: null,

      addressData: {
        //返回数据
        longitude: "",
        latitude: "",
        province: "",
        city: "",
        district: "",
        address: "",
        isMap: false,
      },
      mapMark: null,
    };
  },
  props: {
    isMapList: {
      type: Object,
      default: function () {
        return {};
      },
    },
    centerDefault: {
      type: Array,
      default: () => [116.391253, 39.907166],
    },
    ifMapRuler: {
      //是否开启测距
      type: Boolean,
      default: false,
    },
    lonlatContent: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "地图",
    },
    placeTitle: {
      type: String,
      default: "请输入关键字",
    },
    ifToolBar: {
      type: Boolean,
      default: false,
    },
    showBtn: {
      type: Boolean,
      default: true,
    },
    showSearch: {
      //是否开启搜索
      type: Boolean,
      default: true,
    },
    ifMapMouseTool: {
      //是否开启坐标拾取
      type: Boolean,
      default: true,
    },
  },
  beforeDestroy() {
    measure = null;
  },
  mounted() {
    this.$nextTick(() => {
      this.initCTMap();
    });
  },
  methods: {
    /**
     * 初始化高德2.0版本地图
     */
    initCTMap() {
      $(".search-tips").css({ display: "none" });
      let { map, mapMap } = initCTMap("map", this.centers, 1, 16);
      this.map = map;
      this.mapMap = mapMap;
      this.isMap = true;
      measure = new measureClass(
        map,
        "finish",
        this.turnMapRuler.bind(this),
        true
      );
      this.initMap(map);
    },
    /**
     * 地图初始化
     */
    initMap(map) {
      this.map = map;
      if (
        this.isNotNull(this.isMapList.lng) &&
        this.isNotNull(this.isMapList.lat)
      ) {
        this.getAddress(this.isMapList.lng, this.isMapList.lat, true);
      } else {
        this.getAddress(this.centers[0], this.centers[1], true);
      }
      this.clickDoPoint();
      this.doMapShow();
      addScaleLine(map, "map-scaleLine", 88);
    },
    isNotNull(str) {
      return str !== "" && str != null;
    },
    /**
     * 初始化地图打点
     */
    doMapShow() {
      if (!this.map) {
        $v.message({
          message: "地图加载中，请稍后..!",
        });
      } else {
        if (
          this.isNotNull(this.isMapList.lng) &&
          this.isNotNull(this.isMapList.lat)
        ) {
          //编辑时默认原坐标
          setZoomAndCenter(
            this.map,
            Number(this.isMapList.lng),
            Number(this.isMapList.lat),
            16
          );
          this.doMarker(this.isMapList.lng, this.isMapList.lat);
        } else {
          //新增时默认中心点
          setZoomAndCenter(
            this.map,
            Number(this.centers[0]),
            Number(this.centers[1]),
            16
          );
          this.doMarker(this.centers[0], this.centers[1]);
        }
      }
    },
    /**
     * 地图打点
     */
    doMarker(lng, lat) {
      $(".search-tips").css({ display: "none" });
      if (this.mapMark) {
        this.map.removeOverlay(this.mapMark);
      }
      let imgObj = {
        imgPath: positionIcon,
        imgWidth: 32,
        imgHeight: 32,
        offset: [-16, -32],
      };
      this.mapMark = addMarker(this.map, lng, lat, "mark", imgObj);
      setZoomAndCenter(this.map, Number(lng), Number(lat), 16);
      this.getAddress(lng, lat);
    },
    /**
     * 坐标获取地址
     */
    getAddress(lon, lat) {
      if (!lon || !lat) {
        return false;
      }
      const params = {
        location: lon + "," + lat,
      };
      poiContraryGeocodingQuery(params).then((res) => {
        const _this = this;
        if (res.code === 200) {
          this.addressData.longitude = Number(lon).toFixed(9);
          this.addressData.latitude = Number(lat).toFixed(9);
          let data = res.data[0];
          _this.addressData.address = data.address;
          //兼容address formattedAddress
          _this.addressData.formattedAddress = data.address;
          _this.addressData.province = data.region.province;
          _this.addressData.city = data.region.city;
          _this.addressData.district = data.region.county;
          _this.addressData.township = data.region.town;
          _this.addressData.towncode = data.region.townCode;
        }
      });
    },

    onMapSearchResult(e) {
      setZoomAndCenter(this.map, Number(e.lng), Number(e.lat), 16);
      this.doMarker(e.lng, e.lat);
      this.addressData.longitude = Number(e.lng).toFixed(9);
      this.addressData.latitude = Number(e.lat).toFixed(9);
    },
    /**
     * 测距
     */
    turnMapRuler() {
      if (!this.mapRulerFlag) {
        this.openRule();
      } else {
        this.closeRule();
      }
    },
    /**
     * 开启测距
     */
    openRule() {
      measure.setMeasureType("length");
      measure.startMeasure();
      this.mapRulerFlag = true;
    },
    /**
     * 关闭测距
     */
    closeRule() {
      measure.stopMeasure();
      this.mapRulerFlag = false;
    },
    /**
     * 坐标拾取和打点
     */
    clickDoPoint() {
      if (this.ifMapMouseTool) {
        markerPicking(this.map, (lngLat) => {
          if (this.mapRulerFlag) {
            return false;
          }
          this.doMarker(lngLat[0], lngLat[1]);
        });
      }
    },
    //提交
    submitFun() {
      $(".search-tips").css({ display: "none" });
      this.$emit("isMapCancelFun", this.addressData);
    },
    resetFun() {
      $(".search-tips").css({ display: "none" });
      this.$emit("isMapCancelFun", false);
    },
  },
};
</script>

<style lang="scss" scoped>
/*地图*/
.map-div {
  width: 781px;
  height: 460px;
  position: relative;
  overflow: hidden;
}

/deep/ .el-vue-amap {
  border-radius: 15px !important;
}

.amap-logo {
  display: none !important;
}

.amap-copyright {
  visibility: hidden !important;
}

#my_container {
  height: 500px;
  width: 100%;
}

/deep/ .el-dialog__header .el-dialog__title:before {
  background: white;
}

.el-vue-amap-container.amap-demo {
  height: 500px;
}

.search-box {
  position: absolute !important;
  top: 16px;
  left: 16px;
  width: 280px;
  z-index: 1001;
}

.amap-page-container {
  position: relative;
}
/deep/ .submitClass {
  background: #ff6a6c !important;
  color: #ffffff !important;
  width: 60px;
  font-size: 14px;
  font-weight: 400;
}
.restClass {
  width: 60px;
  font-size: 14px;
  font-weight: 400;
}
/deep/ .el-dialog > .el-dialog__footer {
  padding: 11px 12px 12px 12px;
  border-top: 1px solid #ebeef5;
}
::v-deep .search-tips {
  width: 280px;
  ul {
    width: 278px;
    li {
      overflow: hidden;
    }
  }
}
.map-button-icon {
  border-radius: 4px;
  width: 40px;
  height: 32px;
  background-color: #ffffff;
  color: #ff6a6c;
  font-size: 22px;
  line-height: 32px;
  text-align: center;
  position: absolute;
  top: 16px;
  left: 312px;
  z-index: 3000;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
}
.map-button-icon:hover {
  box-shadow: 1px 2px 10px 3px rgba(0, 0, 0, 0.2);
}
.map-button-icon.checked {
  background-color: #ff6a6c;
  color: #ffffff;
}
.iconfont {
  font-size: 22px;
}
.lon-lat-input {
  height: 32px;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 4px;
  position: absolute;
  z-index: 1;
  right: 12px;
  bottom: 12px;
  padding: 6px 12px;
}
.lon-lat-text {
  height: 20px;
  font-size: 14px;
  font-weight: 400;
  color: #ffffff;
  line-height: 20px;
  text-align: left;
  font-style: normal;
}
/deep/ .amap-logo,
/deep/ .amap-copyright {
  display: none !important;
}
/deep/ .tdt-control-copyright {
  display: none !important;
}
/deep/ .el-dialog > .el-dialog__header {
  padding: 12px;
  border-bottom: 1px solid #ebeef5;
  height: 48px;
}

/deep/ .el-dialog > .el-dialog__body {
  padding: 12px;
}
/*.clear-box {*/
/*  width: 20px;*/
/*  height: 20px;*/
/*  position: absolute;*/
/*  top: 36px;*/
/*  left: 235px;*/
/*  z-index: 12;*/
/*}*/
/deep/ .ctmap-ol-measure-tooltip {
  .__text {
    padding: 3px 6px 3px 8px;
  }
  .__actions {
    svg {
      box-sizing: content-box;
    }
  }
}
/deep/ .map-button-icon {
  width: 32px;
}
.dialog-footer /deep/ .el-button + .el-button {
  margin-left: 8px;
}
</style>
<style lang="scss">
.map-scaleLine {
  position: absolute;
  left: 16px;
  bottom: 15px;
  background: #ffffff;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  padding: 0 10px;
  &:before {
    content: "";
    width: 2px;
    height: 6px;
    pointer-events: none;
    position: absolute;
    left: 9px;
    top: 3px;
    background: #172537;
  }
  &:after {
    content: "";
    width: 2px;
    height: 6px;
    pointer-events: none;
    position: absolute;
    right: 9px;
    top: 3px;
    background: #172537;
  }
  .map-scaleLine-inner {
    margin-top: 5px;
    margin-bottom: 3px;
    padding-top: 2px;
    border-top: 2px solid #172537;
    color: #172537;
    font-size: 10px;
    text-align: center;
    transition: all 0.25s;
    will-change: contents, width;
  }
}
</style>
